
console.log('Demo Run JS.[8001]..');


function autoPlay(){
    var as = $('#v_course a');
    if (as.length > 0) {
        var play = false, click = false;
        for (var i = 0; i < as.length; i++) {
            if (as[i].className == 'course_chapter do') {
                play = true;
            }
            if (play) {
                if(as[i].className == 'course_chapter') {
                    console.log('准备进入：' + as[i].innerHTML)
                    $(as[i]).click();
                    click = true;
                    break;
                }
            }
        }
        if (! play) {
            console.log('[0]准备进入：' + as[0].innerHTML)
            $(as[0]).click();
        } else if (! click) {
            // 重新计算
            reCheckPlay(as);
        }
        
    } else {
        alert('页面数据异常...');
    }
}

function reCheckPlay(as){
    console.log('开始重新检测播放列表:' + as.length);
    var play = false;
    for (var i = 0; i < as.length; i++) {
        if (as[i].className != 'course_chapter over') {
            play = true;
            console.log('[rc]准备进入：' + as[i].innerHTML)
            $(as[i]).click();
            break;
        }
    }
    if (! play) {
        console.log('貌似播放完了...', as);
        alert('貌似已经播放完了哦.');
    }
}

console.log('准备监听通知消息');

window.addEventListener('message', function(event) { 
    
    console.log('收到通知消息. ', event);
    
    autoPlay();
    
});

console.log('已经监听通知消息');


